package com.xing.controller;

import java.util.List;
import android.content.Context;
import android.os.Handler;
import android.view.View;
import com.xing.dao.DBDao;
import com.xing.model.HsdAccount;
import com.xing.model.HsdDataResearch;
import com.xing.model.HsdDataResult;
import com.xing.model.HsdModelAnswer;
import com.xing.model.HsdModelQuestion;
import com.xing.model.HsdModelSurvey;
import com.xing.model.HsdQuestionAndAnswerModelListObj;
import com.xing.model.HsdUploadDetail;
import com.xing.model.UploadDetail;
import com.xing.my_model.SurveyListEntity;
import com.xing.util.HsdProgressDialog;
import com.xing.util.MyExecutorService;
import com.xing.util.MyLog;

public class DBController extends DBBaseController{

	
	private static DBController instance = null;

	public static DBController getInstance() {
		if (instance == null) {
			instance = new DBController();
		}
		return instance;
	}
	/**
	 * 
	 * @param context
	 * @param handler
	 */
	public void getHsdModelSurveyList(final Context context,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				List<HsdModelSurvey> list  = null;
				try {
					list = DBDao.getHsdModelSurveyList(context);
				} catch (Exception e) {
					// TODO: handle exception
					list = null;
					System.out.println("excep---getHsdModelSurveyList-->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(list, context, handler);
//					dimissProgressDialog(progressDialog);
				}
			}
		});
	}
	
	public void getHsdModeSurveyQuestoinNum(final Context context,final String surveyuuid,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Integer questionCount = null;
				try {
					questionCount = DBDao.getHsdModeSurveyQuestoinNum(context, surveyuuid);
				} catch (Exception e) {
					// TODO: handle exception
					questionCount = null;
					System.out.println("excep---getHsdModeSurveyQuestoinNum-->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(questionCount, context, handler);
				}
			}
		});
	}
	/**
	 * 得到问卷问题模型信息
	 * @param surveyuuid
	 * @param currentSortIndex
	 * @param context
	 * @param handler
	 */
	public void getHsdModelQuestionObject(final String surveyuuid,final String currentSortIndex,final Context context,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				HsdModelQuestion obj  = null;
				try {
					obj = DBDao.getHsdModelQuestionObjec(surveyuuid, currentSortIndex, context);
				} catch (Exception e) {
					// TODO: handle exception
					obj = null;
					System.out.println("excep----->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(obj, context, handler);
				}
			}
		});
	}
	/**
	 * 得到子问题
	 * @param surveyuuid
	 * @param fatherquestionuuid
	 * @param context
	 * @param handler
	 */
	public void getChildHsdModelQuestionObjec(final String surveyuuid,final String fatherquestionuuid,final Context context,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				List<HsdModelQuestion> list  = null;
				try {
					list = DBDao.getChildHsdModelQuestionObjec(surveyuuid, fatherquestionuuid, context);
				} catch (Exception e) {
					// TODO: handle exception
					list = null;
					MyLog.outLogDetail(e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(list, context, handler);
				}
			}
		});
	}
	
	/**
	 * 得到问题答案模型
	 * @param questionuuid
	 * @param surveyuuid
	 * @param context
	 * @param handler
	 */
	public void getHsdModelAnswerList(final String questionuuid,final String surveyuuid,final Context context,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);;
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				List<HsdModelAnswer> list = null;
				try {
					list = DBDao.getHsdModelAnswerList(questionuuid, surveyuuid, context);
				} catch (Exception e) {
					// TODO: handle exception
					list = null;
					System.out.println("excep---getHsdModelAnswerList-->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(list, context, handler);
				}
			}
		});
	}
	
	
	
	
	public void insertHsdQuestionAndAnswerModelListObj(final Context context,final HsdQuestionAndAnswerModelListObj hsdQuestionModelListObj,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);;
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				boolean isInsert = false;
				try {
					isInsert = DBDao.insertHsdQuestionAndAnswerModelListObj(context, hsdQuestionModelListObj);
				} catch (Exception e) {
					// TODO: handle exception
					isInsert = false;
					System.out.println("excep---insertHsdQuestionAndAnswerModelListObj-->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(isInsert, context, handler);
				}
			}
		});
	}
	
	/**
	 * 插入问题（或者子问题）的所选结果   插入的时候要有questionuuid，fatherquestionuuid不需要，每个问题或者子问题的questinuuid是唯一的。
	 * @param researchuuid
	 * @param surveyuuid
	 * @param answeruuid
	 * @param questionuuid
	 * @param answertext
	 * @param context
	 * @param handler
	 */
	public void insertQuestionResult(final String researchuuid,final String surveyuuid,final String answeruuid,final String questionuuid,final String answertext,final Context context
			,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);;
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				boolean isInsertSucess = false;
				try {
					isInsertSucess = DBDao.insertQuestionResult(researchuuid, surveyuuid,answeruuid, questionuuid, answertext, context);
				} catch (Exception e) {
					// TODO: handle exception
					isInsertSucess = false;
					System.out.println("excep----->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(isInsertSucess, context, handler);
				}
			}
		});
	}
	/**
	 * mutiple question type
	 * 
	 * delete result  的时候不能根据 questionuuid,如果按照questionuuid的话,整个答案全部删除了.
	 * @param researchuuid
	 * @param surveyuuid
	 * @param answeruuid
	 * @param questionuuid
	 * @param context
	 * @param handler
	 */
	public void deleteQuestionResult(final String researchuuid,final String surveyuuid,final String answeruuid,final Context context,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);;
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				boolean isDeleteSucess = false;
				try {
					isDeleteSucess = DBDao.deleteQuestionResult(researchuuid, surveyuuid, answeruuid,context);
				} catch (Exception e) {
					// TODO: handle exception
					isDeleteSucess = false;
					System.out.println("deleteQuestionResult-excep----->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(isDeleteSucess, context, handler);
				}
			}
		});
	}

	
	/**
	 * 更新问题(单纯的一个问题,或者是子问题)答案
	 * @param researchuuid
	 * @param surveyuuid
	 * @param answeruuid
	 * @param questionuuid
	 * @param answertext
	 * @param context
	 * @param handler
	 */
	public void updateQuestionResult(final String researchuuid,final String surveyuuid,final String answeruuid,final String questionuuid,final String answertext,final Context context,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);;
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				boolean isUpdateSucess = false;
				try {
					isUpdateSucess = DBDao.updateQuestionResult(researchuuid, surveyuuid, answeruuid, questionuuid, answertext, context);
				} catch (Exception e) {
					// TODO: handle exception
					isUpdateSucess = false;
					System.out.println("updateQuestionResult-excep----->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(isUpdateSucess, context, handler);
				}
			}
		});
	}
	/**
	 * 得到问题（只是单纯的问题）问卷问题的所选结果（问卷详情页结果显示）
	 * @param researchuuid
	 * @param surveyuuid
	 * @param questionuuid
	 * @param context
	 * @param handler
	 */
	public void getHsdDataResultList(final String researchuuid,final String surveyuuid,final String questionuuid,final Context context,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				List<HsdDataResult> list = null;
				try {
					list = DBDao.getHsdDataResultList(researchuuid, surveyuuid, questionuuid, context);
				} catch (Exception e) {
					// TODO: handle exception
					list = null;
					System.out.println("getHsdDataResult excep----->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(list, context, handler);
				}
			}
		});
	}
	/**
	 * 得到问卷模型的surveyuuid list
	 * @param context
	 * @param handler
	 */
	public void getHsdModelSurveyuuidList(final Context context,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);;
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				List<String> list = null;
				try {
					list = DBDao.getHsdModelSurveyuuidList(context);
				} catch (Exception e) {
					// TODO: handle exception
					list = null;
					System.out.println("getHsdModelSurveyuuidList excep----->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(list, context, handler);
				}
			}
		});
	}

	public void getSurveyListEntity(final Context context,final String surveyuuid,final View hasFinishedView,final View notFinishedView,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);;
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				SurveyListEntity surveyListEntity = null;
				try {
					surveyListEntity = DBDao.getSurveyListEntity(context, surveyuuid, hasFinishedView, notFinishedView);
				} catch (Exception e) {
					// TODO: handle exception
					surveyListEntity = null;
					System.out.println("getSurveyListEntity excep----->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(surveyListEntity, context, handler);
//					dimissProgressDialog(progressDialog);
				}
			}
		});
	}
	/**
	 * 得到本地问卷的上传状态(已上传和未上传数)
	 * @param context
	 * @param surveyuuid
	 * @param handler
	 */
	public void getResearchUpStatus(final Context context,final String surveyuuid,final View has_uploaded_view,final View not_upload_view,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);;
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				UploadDetail uploadDetail = null;
				try {
					uploadDetail = DBDao.getResearchUpStatus(context,has_uploaded_view,not_upload_view,surveyuuid);
				} catch (Exception e) {
					// TODO: handle exception
					uploadDetail = null;
					System.out.println("getSurveUploadDetail excep----->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(uploadDetail, context, handler);
				}
			}
		});
	}
	
	
	/**
	 * 得到未上传的问卷的researchlist，然后将这些上传到服务器
	 * @param context
	 * @param surveyuuid
	 * @param handler
	 */
	public void getHsdDataResearchNotUploadList(final Context context,final String surveyuuid,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);;
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				List<HsdDataResearch> list = null;
				try {
					list = DBDao.getHsdDataResearchNotUploadList(context, surveyuuid);
				} catch (Exception e) {
					// TODO: handle exception
					list = null;
					System.out.println("getHsdDataResearch excep----->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(list, context, handler);
//					dimissProgressDialog(progressDialog);
				}
			}
		});
	}
	/**
	 * 查询要向服务回上传的结果数据对象
	 * @param context
	 * @param hsdUploadResearch
	 * @param surveyuuid
	 * @param researchuuid
	 * @param handler
	 */
	public void  getHsdUploadDetail(final Context context,final HsdDataResearch hsdDataResearch,final String surveyuuid,final String researchuuid,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);;
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				HsdUploadDetail hsdUploadDetail  = null;
				try {
					hsdUploadDetail = DBDao.getHsdUploadDetail(context,hsdDataResearch,surveyuuid,researchuuid);
				} catch (Exception e) {
					// TODO: handle exception
					hsdUploadDetail = null;
					System.out.println("getHsdDataResearch excep----->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(hsdUploadDetail, context, handler);
				}
			}
		});
	}
	
	/**
	 * 更新research table 的上传状态
	 * @param context
	 * @param surveyuuid
	 * @param researchuuid
	 * @param handler
	 */
	public void updateHsdDataResearchUpStatus(final Context context,final String surveyuuid,final String researchuuid,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);;
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				boolean	 isUpdate = false;
				try {
					isUpdate = DBDao.updateHsdDataResearchUpStatus(context, surveyuuid, researchuuid);
				} catch (Exception e) {
					// TODO: handle exception
					isUpdate = false;
					System.out.println("getHsdDataResearch excep----->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(isUpdate, context, handler);
				}
			}
		});
	}
	 /**
     * 更新本地问卷状态
	 * 问卷状态
	 * 1 not download
	 * 2:has downloaded
	 * 3:out of date
	 * 4:has deleted
	 *
    * @param context
    * @param surveyuuid
    * @return
    * @throws Exception
    */
	public void updateHsdModelSurveyStatus(final Context context,final String surveyuuid,final int status,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);;
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				boolean	 isUpdate = false;
				try {
					isUpdate = DBDao.updateHsdModelSurveyStatus(context, surveyuuid, status);
				} catch (Exception e) {
					// TODO: handle exception
					isUpdate = false;
					System.out.println("updateHsdModelSurveyStatus excep----->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(isUpdate, context, handler);
				}
			}
		});
	}
	
	/**
	 *交卷
	 * @param context
	 * @param surveyuuid
	 * @param researchuuid
	 * @param handler
	 */
	public void updateHsdDataResearchFinishStatus(final Context context,final String surveyuuid,final String researchuuid,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);;
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				boolean	 isUpdate = false;
				try {
					isUpdate = DBDao.updateHsdDataResearchFinishStatus(context, surveyuuid, researchuuid);
				} catch (Exception e) {
					// TODO: handle exception
					isUpdate = false;
					System.out.println("updateHsdDataResearchFinishStatus excep----->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(isUpdate, context, handler);
				}
			}
		});
	}
	/**
	 * 新建问卷
	 * @param context
	 * @param surveyuuid
	 * @param name
	 * @param sex
	 * @param age
	 * @param telephone
	 * @param mobilephone
	 * @param email
	 * @param documenttype
	 * @param idnumber
	 * @param researchaccount
	 * @param researchname
	 * @param createtime
	 * @param modifytime
	 * @param handler
	 */
	public void insertHsdDataResearch(final Context context,final String researchuuid,final String surveyuuid,final String name,final int sex,final int age,final String telephone,final String mobilephone
			   ,final String email,final int documenttype,final String idnumber,final String researchaccount,final String researchname,final String createtime,final String modifytime,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);;
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				boolean	 isInsert = false;
				try {
					isInsert = DBDao.insertHsdDataResearch(context,researchuuid,surveyuuid, name, sex, age, telephone, mobilephone, email, documenttype, idnumber, researchaccount, researchname, createtime, modifytime);
				} catch (Exception e) {
					// TODO: handle exception
					isInsert = false;
					System.out.println("getHsdDataResearch excep----->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(isInsert, context, handler);
				}
			}
		});
	}
	
	/**
	 * 得到hsdDataResearch （SurveyListSecondFragment page）
	 * @param context
	 * @param surveyuuid
	 * @param handler
	 */
	public void getHsdDataResearchListInDetail(final Context context,final String surveyuuid,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);;
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				List<HsdDataResearch> list = null;
				try {
					list = DBDao.getHsdDataResearchListInDetail(context, surveyuuid);
				} catch (Exception e) {
					// TODO: handle exception
					list = null;
					System.out.println("getHsdDataResearchListInDetail excep----->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(list, context, handler);
				}
			}
		});
	}
	
	public void insertHsdAccount(final Context context,final String username,final String password,final String loginid,final String token,
			final Handler handler){
		HsdProgressDialog.getProgressDialog(context);;
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				boolean isInsert = false;
				try {
					isInsert = DBDao.insertHsdAccount(context, username, password, loginid, token);
				} catch (Exception e) {
					// TODO: handle exception
					isInsert = false;
					System.out.println("insertHsdAccount excep----->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(isInsert, context, handler);
				}
			}
		});
	}
	
	public void getHsdAccount(final Context context,final String loginid,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);;
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				HsdAccount hsdAccount = null;
				try {
					hsdAccount = DBDao.getHsdAccount(context, loginid);
				} catch (Exception e) {
					// TODO: handle exception
					hsdAccount = null;
					System.out.println("getToken excep----->"+e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessageData(hsdAccount, context, handler);
				}
			}
		});
	}
}
